package lv.ante.xwiki.filters;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class MimeTypeFilter implements Filter {

        /**
         * The filter configuration object we are associated with. If this value is
         * null, this filter instance is not currently configured.
         */
        protected FilterConfig filterConfig = null;

        private String delimiter;
        private String paramKey;
        private String paramVals;
        private List<String> paramValList = new ArrayList<String>();
        private String mimeTypes;
        private List<String> mimeTypeList = new ArrayList<String>();

        public void init(FilterConfig filterConfig) throws ServletException {
                
                System.err.println("MimeTypeFilter.init()");
                this.filterConfig = filterConfig;
                this.delimiter = filterConfig.getInitParameter("delimiter");
                this.paramKey = filterConfig.getInitParameter("paramKey");
                this.paramVals = filterConfig.getInitParameter("paramVals");
                this.mimeTypes = filterConfig.getInitParameter("mimeTypes");
                if (paramVals != null) {
                        paramValList = Arrays.asList(paramVals.split(","));
                }
                if (mimeTypeList != null) {
                        mimeTypeList = Arrays.asList(mimeTypes.split(","));
                }
        }

        public void doFilter(ServletRequest req, ServletResponse res,
                        FilterChain chain) throws IOException, ServletException {
                HttpServletRequest request = (HttpServletRequest) req;
                HttpServletResponse response = (HttpServletResponse) res;
                String xpage = request.getParameter(paramKey);
                String mime = null;
                if (xpage != null && paramValList.contains(xpage)) {
                        int index = paramValList.indexOf(xpage);
                        if (index != -1 && index < mimeTypeList.size()) {
                                mime = mimeTypeList.get(index);
                        }
                }
                if (mime != null) {
                        request.setCharacterEncoding("UTF-8");
                        chain.doFilter(request, new MimeTypeResponseWrapper(response,
                                        delimiter, mime));
                } else {
                        // return unmodified response (its Content-type is set by servlet)
                        request.setCharacterEncoding("UTF-8");
                        chain.doFilter(request, response);
                }
        }

        public void destroy() {
                System.err.println("MimeTypeFilter.destroy()");
        }
}
